package com.shudingke.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shudingke.domain.Collections;
import com.shudingke.domain.Products;
import com.shudingke.service.ICollectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/collections")
public class CollectionsController {
    @Autowired
    ICollectionsService service;
    //进入收藏界面一定要访问的方法
    //先从session域中获取到用户的id   如果没有获取到id表明该用户还没有登录
    @RequestMapping("/myCollections")
    public String myCollections(@RequestParam(defaultValue = "")String searchName,Model model,
                                HttpServletRequest req,
                                @RequestParam(defaultValue = "1") String page){
        model.addAttribute("searchName",searchName);
        String uuid = req.getSession().getAttribute("uid").toString();
        List<Products> myCollections = null;
        PageInfo<Products> info = null;
        if (!StringUtils.isEmpty(uuid)){
            if (StringUtils.isEmpty(searchName)){
                searchName = null;
            }else {
                searchName = "%" + searchName + "%";
            }
            int uid = Integer.parseInt(uuid);
            //根据用户id进行三表查询查询出用户收藏的产品
            PageHelper.startPage(Integer.parseInt(page),12);
            myCollections = service.findAll(uid,searchName);
            info = new PageInfo<>(myCollections);
            
        }
        model.addAttribute("info",info);
        int pages = info.getPages();
        List pList = new ArrayList();
        //把页码存到集合里面去
        for (int i = 1; i <= pages; i++){
            pList.add(i);
        }
        model.addAttribute("pList", pList);

        List<Products> records = info.getList();
        
        //model.addAttribute("records", records);

        //把每行可能要遍历的对象存到集合中

        List<Products> list1 = new ArrayList();
        List<Products> list2 = new ArrayList();
        List<Products> list3 = new ArrayList();
        for (int i = 0; i < records.size(); i++){
            if (i < 4){
                list1.add(records.get(i));
            }else if (4 <= i && i < 8){
                list2.add(records.get(i));
            }else if (8 <= i && i < 12){
                list3.add(records.get(i));
            }
        }
        model.addAttribute("list1", list1);
        model.addAttribute("list2", list2);
        model.addAttribute("list3", list3);
        
        
        
        
        return "favorites";
    }
    
    //添加商品到我的收藏时要进入的方法
    @RequestMapping("/addToCollections")
    public String addToCollections(String id,HttpServletRequest req,Model model){
        //获取用户id
        String uuid = req.getSession().getAttribute("uid").toString();
        //获取产品id
        int pid = Integer.parseInt(id);
        int uid = Integer.parseInt(uuid);
        //创建对象
        Collections col = new Collections();
        col.setPid(pid);
        col.setUid(uid);
        
        //添加之前要先进行判断是否已经存在收藏里面
        QueryWrapper<Collections> qw = new QueryWrapper<>();
        qw.eq("pid",pid).eq("uid",uid);
        Collections one = service.getOne(qw);
        if (one == null){
            //调用内置sql语句
            service.save(col);    
        }
        //调用方法跳转到收藏页面
        return "redirect:myCollections";
    }


    //从我的收藏取消收藏商品要进入的方法
    @RequestMapping("/removeCollections")
    public String removeCollections(String id,HttpServletRequest req,Model model){
        //获取用户id
        String uuid = req.getSession().getAttribute("uid").toString();
        //获取产品id
        int pid = Integer.parseInt(id);
        int uid = Integer.parseInt(uuid);
        QueryWrapper<Collections> qw = new QueryWrapper<>();
        qw.eq("uid",uid).eq("pid",pid);
        //调用内置sql语句
        service.remove(qw);
        //调用方法跳转到收藏页面
        return "redirect:myCollections";
    }
}
